package com.lkx.sms.activity;

import java.util.List;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.TextView;
import com.lkx.R;
import com.lkx.sms.data.SmsSession;
import com.lkx.sms.data.SmsSessionListAdapter;
import com.lkx.sms.tool.SmsResolver;

public class SmsList extends ListActivity{

	private List<SmsSession> smsSessions;
	private TextView empty;
	private ProgressDialog progressDialog;
	private final Handler smsHandler = new Handler() {
		@Override
		public void handleMessage(final Message msg) {
			if(smsSessions==null||smsSessions.size()==0) {
				getListView().setEmptyView(empty);
			}else {
				setListAdapter(new SmsSessionListAdapter(SmsList.this, smsSessions));
			}
			progressDialog.dismiss();
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_smslist);
		empty = (TextView) findViewById(R.id.empty);
		ListView listView = getListView();
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		listView.setEmptyView(empty);
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadSmsSessions();
	}

	private void loadSmsSessions() {
		progressDialog = ProgressDialog.show(this, "", "Please Wait...", true, false);
		new Thread() {
			@Override
			public void run() {
				smsSessions = SmsResolver.getSmsSessionList(getContentResolver());
				smsHandler.sendEmptyMessage(0);
			}
		}.start();
	}
}
